package com.baizhi.service.impl;

import com.baizhi.dao.AddressDao;
import com.baizhi.entity.Address;
import com.baizhi.service.AddressService;
import com.baizhi.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class AddressServiceImpl implements AddressService {
    @Override
    public List<Address> selectUserAllAddressByUserId(String userId) {
        List<Address> addresses=new ArrayList<>();
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            AddressDao addressDao = sqlSession.getMapper(AddressDao.class);
            addresses=addressDao.selectUserAllAddressByUserId(userId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
        return addresses;
    }

    @Override
    public void addAddress(Address address) {
        SqlSession sqlSession= MyBatisUtils.openSession();
        try {
            AddressDao addressDao = sqlSession.getMapper(AddressDao.class);
            addressDao.addAddress(address);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }
        finally {
            MyBatisUtils.close(sqlSession);
        }
    }
}
